package com.moonfish.base.model.menu;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.moonfish.base.model.i18n.International;
import com.moonfish.base.model.security.Role;
import lombok.*;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by mayu on 2017/7/10.
 * email:showmara@qq.com
 */
@Data
@Entity
//@ToString(exclude = {"i18n","authorities","parent","children"})
@ToString(exclude = {"i18n","authorities"})
@Table(name = "MENU")
public class Menu {

    @Id
    @Column(name = "ID",length = 32)
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    private String id;

    @Column(name = "NAME")
    private String name;

    @Column(name = "ICON",length = 20)
    private String icon;

    @Column(name = "PATH",length = 50)
    private String path;

    @Column(name = "EXPANDED")
    private boolean expanded;

    @Column(name = "LINK")
    private String link;

    @Column(name = "LABEL")
    private String label;

    @Column(name = "DESCRIPTION")
    private String description;

    @Column(name = "REPOSITORY")
    private String repository;

    @Column(name = "PID",length = 32)
    private String pid;

    @Column(name = "LEVEL")
    private int level;

    @JsonIgnore
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = "MENU_AUTHORITY",
            joinColumns = {@JoinColumn(name = "MENU_ID", referencedColumnName = "ID")},
            inverseJoinColumns = {@JoinColumn(name = "AUTHORITY_ID", referencedColumnName = "ID")})
    private List<Role> authorities;

//    @JsonIgnore
    @OneToMany(fetch = FetchType.LAZY,cascade=CascadeType.ALL)
    @JoinTable(
            name = "MENU_INTERNATIONAL",
            joinColumns = {@JoinColumn(name = "MENU_ID", referencedColumnName = "ID")},
            inverseJoinColumns = {@JoinColumn(name = "INTERNATIONAL_ID", referencedColumnName = "ID")})
    private List<International> i18n;



//    @ManyToOne(fetch = FetchType.EAGER)
//    @JoinColumn(name = "PID")
//    private Menu parent;
//
//    @OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL,mappedBy = "parent")
//    private Set<Menu> children = new HashSet<Menu>();
    @Transient
    private List<Menu> children = new ArrayList<Menu>();
}
